#include "stm32f10x.h"
#include "usart.h"
#include "key.h"
#include "tim.h"
#include "i2c.h"
#include "stdio.h"
#include "lcd.h"
#include "led.h"

u32 TimingDelay = 0;
extern u8 PA1_state;
extern u8 PA2_state;
extern u8 pulse1;
extern u8 pulse2;
extern char str[20];
extern u8 cmd_flag;
extern u32 time;
extern	u8 i;
char cmd[20];

//Main Body
int main(void)
{
	char text[30];
	i2c_init();
	SysTick_Config(SystemCoreClock/1000);
	STM3210B_LCD_Init();
  LCD_SetTextColor(White);
  LCD_SetBackColor(Blue2);
  LCD_Clear(Blue2);
	led_init();
	tim3_init();
	pwm_init();
	key_init();
	usart_init();
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	while(1)
	{
		state_get();
		if(PA1_state == PA1_output_low)
			led_ctrl(led1,1);
		else if(PA1_state == PA1_output_pwm){
			sprintf(text,"Channel:PA1  ");
			LCD_DisplayStringLine( Line5,text);
			led_ctrl(led1,0);
		}
		if(PA2_state == PA2_output_low)
			led_ctrl(led2,1);
		else if(PA2_state == PA2_output_pwm){
			sprintf(text,"Channel:PA2  ");
			LCD_DisplayStringLine( Line5,text);
			led_ctrl(led2,0);
		}
		if((PA1_state==PA1_output_low)&&(PA2_state == PA2_output_low))
			sprintf(text,"Channel: None");
			LCD_DisplayStringLine( Line5,text);
		sprintf(text,"PWM-PA1:%1d0%%   ",pulse1);
		LCD_DisplayStringLine( Line2,text);
		sprintf(text,"PWM-PA2:%1d0%%   ",pulse2);
		LCD_DisplayStringLine( Line3,text);
		sprintf(text,"Time:%2d:%2d:%2d  ",time/3600,(time%3600)/60,time%60);
		LCD_DisplayStringLine( Line4,text);
		if(cmd_flag == 1){
			LCD_DisplayStringLine( Line6,"Command:");
			sprintf(text,"     %s",str);
			LCD_DisplayStringLine( Line7,text);
			for(i = 0;i<20;i++)
				str[i] = 0;
		}
		else if(cmd_flag == 0){
			LCD_DisplayStringLine( Line6,"Command:");
			LCD_DisplayStringLine( Line7,"           None");
		}
	}
}


